package processes

import (
	"encoding/json"
	"fmt"
	"learningProject/shangguigu/client/utils"
	message "learningProject/shangguigu/common"
	"net"
)

type SmsProcess struct {
}

// 转发消息
func (this *SmsProcess) SendGroupMes(mes *message.Message) {
	var smsMes message.SmsMes
	err := json.Unmarshal([]byte(mes.Data), &smsMes)
	if err != nil {
		fmt.Println("服务器转发  json.Unmarshal err")
	}

	data, err := json.Marshal(mes) // 将mes再重新序列化回去
	if err != nil {
		fmt.Println("服务器转发  json.Marshal err")
	}
	// 遍历服务器端的map,将消息转发出去
	for id, up := range userMgr.onlineUsers {
		// 过滤掉自己
		if id == smsMes.UserId {
			continue
		}
		this.SendMesToEachOnlineUser(data, up.Conn)
	}
}

func (this *SmsProcess) SendMesToEachOnlineUser(data []byte, conn net.Conn) {
	// 创建transfer实例，转发消息
	tf := &utils.Transfer{
		Conn: conn,
	}
	err := tf.WritePkg(data)
	if err != nil {
		fmt.Println("SendMesToEachOnlineUser err")
	}
}
